package cn.wsalix.admin.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.anlaser.service.impl.RelatedServiceImpl;
import cn.wsalix.admin.entity.SysRole;
import cn.wsalix.admin.entity.SysUser;
import cn.wsalix.admin.entity.UserRole;
import cn.wsalix.admin.form.UserRoleForm;
import cn.wsalix.admin.repository.UserRoleRepository;
import cn.wsalix.admin.service.RoleService;
import cn.wsalix.admin.service.UserRoleService;
import cn.wsalix.admin.service.UserService;
import cn.anlaser.utils.SearchFilter;
import cn.anlaser.utils.SearchFilter.Operator;

@Service("userRoleService")
public class UserRoleServiceImpl
		extends
		RelatedServiceImpl<UserRoleForm, UserRoleRepository, UserRole, SysUser, SysRole>
		implements UserRoleService {
	@Autowired
	private UserService userService;
	@Autowired
	private RoleService roleService;
	@Autowired
	private UserRoleRepository userRoleRepository;
	
	@Override
	public SysUser findPreFix(Long preFixId) {
		return userService.findById(preFixId);
	}

	@Override
	public SysRole findPostFix(Long postFixId) {
		return roleService.findById(postFixId);
	}

	@CacheEvict(value = "myMenuCache", key = "#form.getId()")
	@Transactional
	@Override
	public void removeRelated(UserRoleForm form) {
		if (form.getId() != null) {
			userRoleRepository.delete(form.getId());
		} else {
			List<UserRole> userRoles = findByForm(form);
			userRoleRepository.delete(userRoles);
		}
	}

	/*
	 * @Override public UserRole setRelated(UserRoleForm form) {
	 * form.setUser(findPreFix(form.getPreFixId()));
	 * form.setRole(findPostFix(form.getPostFixId())); return
	 * super.setRelated(form); }
	 */
	@Transactional
	@Override
	public UserRole setRelated(UserRoleForm form) {
		userService.updateUserRole(form.getUserId());
		return super.setRelated(form);
	}

	@Override
	public List<UserRole> findRelateds(UserRole entity) {
		if (entity.getPreFixId() != null && entity.getPreFixId() != 0) {
			return userRoleRepository.findByUserId(entity.getPreFixId());
		}
		if (entity.getPostFixId() != null && entity.getPostFixId() != 0) {
			return userRoleRepository.findByRoleId(entity.getPostFixId());
		}
		return null;
	}

	@Override
	public List<SysUser> findUnPreFix(Long roleId) {
		return userService.findUnUsersByRoleId(roleId);
	}

	@Override
	public List<SysRole> findUnPostFix(Long userId) {
		return roleService.findUnRolesByUserId(userId);
	}

	@Override
	public UserRoleRepository getRepository() {
		return userRoleRepository;
	}

	@Override
	public Map<String, SearchFilter> buildByForm(UserRoleForm form) {
		Map<String, Object> searchParams = new HashMap<String, Object>();
		Map<String, SearchFilter> filters = SearchFilter.parse(searchParams);
		if (form.getRoleId() != null && form.getRoleId() != 0) {
			filters.put("role.id", new SearchFilter("role.id", Operator.EQ,
					form.getRoleId()));
		}
		if (form.getUserId() != null && form.getUserId() != 0) {
			filters.put("user.id", new SearchFilter("user.id", Operator.EQ,
					form.getUserId()));
		}
		return filters;
	}

	@Override
	public String module() {
		return "/admin/user_role";
	}

	@Override
	public Class<UserRole> getEntityClazz() {
		return UserRole.class;
	}

}
